package com.example.demo.java8;

import java.util.*;

/**
 * @ClassName Test
 * @Description TODO
 * @Author hly
 * @Date 2018\11\28 0028 14:25
 */
public class Test {

    public void test01(){
        // 1.1 匿名内部类的写法一
        Comparator<String> comparator = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(),o2.length());
            }
        };
        TreeSet<String> treeSet = new TreeSet<>(comparator);

        // 1.2 匿名内部类写法二
        TreeSet<String> treeSet2 = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return Integer.compare(o1.length(),o2.length());
            }
        });

        //1.3当使用 Lambda 表达式后，更加简化
        Comparator<String> comparator1 = (x,y)->Integer.compare(x.length(),y.length());


    }

    public List<Employee> screenEmployeeByAge(List<Employee> emps){
        List<Employee> list = new ArrayList<>();
        for (Employee employee : emps) {
            if(employee.getAge() <= 40){
                list.add(employee);
            }
        }
        return list;
    }



    public static void main(String[] args) {
        List<Employee> emps = Arrays.asList(
                new Employee(001, "Emma", 41, 20000, "1"),
                new Employee(002, "Mary", 39, 18000, "1"),
                new Employee(003, "Allen", 33, 15000, "1"),
                new Employee(004, "Olivia", 52, 32000, "1"),
                new Employee(005, "Natasha", 27, 13000,"1"),
                new Employee(006, "Kevin", 25, 10000,"1")
        );

        Test test = new Test();
        //普通方式
        test.screenEmployeeByAge(emps).forEach(System.out::println);
        //使用 Stream API
        emps.stream().filter((e)->e.getAge()<40).forEach(System.out::println);
    }

}
